// pages/openbillbox/receiveaddress/receiveaddress.js
import {
  textToAddressInfo
} from '../../../utils/networkRequest.js'
import { errorMsg} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    phone: "",
    shenShiQu: [],
    address: "",
    text: "",
  },
  onLoad:function(options){
    if (options.shouAddressInfo){
      let shouAddressInfo = JSON.parse(options.shouAddressInfo);
      this.setData({
        name: shouAddressInfo.name,
        shenShiQu: shouAddressInfo.shenShiQu,
        address: shouAddressInfo.address,
        phone: shouAddressInfo.phone
      })
    }
    
  },
  //获取收件人姓名
  getName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //获取收件人电话
  getPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取收件人所在省市区
  getShenShiQu: function(e) {
    this.setData({
      shenShiQu: e.detail.value
    })
  },
  //获取收件人详细地址
  getAddress: function(e) {
    this.setData({
      address: e.detail.value
    })
  },
  //获取自动识别文字
  getText: function(e) {
    this.setData({
      text: e.detail.value
    })
  },
  //清空
  textClear: function() {
    this.setData({
      text: ""
    })
  },
  //自动识别地址信息自动拆封
  textToAddressInfo: function() {
    let self = this;
    let {
      text
    } = this.data;
    if (text == "") {
      errorMsg("请先粘贴地址信息")
    } else {
      textToAddressInfo(text, (r) => {
        console.log(r);
        if (r.code == 0) {
          self.setData({
            name: r.data.name,
            phone: r.data.mobile,
            shenShiQu: r.data.p_c_b,
            address: r.data.address
          })
        } else {
          r.message&& errorMsg(r.message)
        }
      })
    }

  },
  //收货地址填写
  addressSub: function() {
    let {
      name,
      phone,
      shenShiQu,
      address
    } = this.data;

    let shouAddressInfo = {
      name:name,
      phone:phone,
      shenShiQu: shenShiQu,
      address:address
    }
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面 
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去  
    
    prevPage.setData({
      shouAddressInfo: shouAddressInfo
    })
    wx.navigateBack(); 

    
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      shenShiQu: e.detail.value
    })
  }

})